/*
* @Author: mz_zml
* @Date:   2018-03-31 15:55:19
* @Last Modified by:   mz_zml
* @Last Modified time: 2018-07-15 17:25:43
*/
;(function () {
  function Detail() { 
    this.dictList = []
    this.id = null
    this.scrollStatus = false
    this.setTimer = null
    this.downloadStatus = false
    this.lastModifyTime = null
    this.setInter = null
  }

  Detail.prototype.init = function () {
    var that = this
    this.rem()
    var id = parseInt(this.getQueryString('id'))
    if (id) {
      this.id = id
      this.getDict()
    }

    $(document).on('scroll', function (e) {
      clearTimeout(that.setTimer)
      that.setTimer = setTimeout(function () {
        var scrollTop = $(document).scrollTop()
        if (scrollTop > 50) {
          if (that.downloadStatus) {
            return
          }
          that.downloadStatus = true
          $('.download').stop().fadeIn()
        } else {
          that.downloadStatus = false
          $('.download').stop().fadeOut()
        }
        that.scrollStatus = false
      }, 100)
    })
  }

  Detail.prototype.getQueryString = function (name) { 
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i")
    var r = window.location.search.substr(1).match(reg)
    if (r != null) 
      return unescape(r[2])
    return null; 
  } 

  Detail.prototype.countdown = function () {
    var that = this,
        lastModifyTime = that.lastModifyTime + 1000 * 60 * 10,
        currentTime = +new Date(),
        countdownTime = (lastModifyTime - currentTime) / 1000

    if (currentTime > lastModifyTime) {
      setTimeout(function () {
        $('.detail-header-timer').html('已过期')
      }, 0)
    } else {
      that.countdownHandle(countdownTime)
    }
  }

  Detail.prototype.countdownHandle = function (times) {
    var timer = null
    this.setInter = setInterval(function () {
      var html = ''
      var day = hour = minute = second = 0
      if (times > 0) {
        day = Math.floor(times / (60 * 60 * 24))
        hour = Math.floor(times / (60 * 60)) - (day * 24)
        minute = Math.floor(times / 60) - (day * 24 * 60) - (hour * 60)
        second = Math.floor(times) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60)
      }
      if (day <= 9) day = '0' + day
      if (hour <= 9) hour = '0' + hour
      if (minute <= 9) minute = '0' + minute
      if (second <= 9) second = '0' + second
      //
      if (day != '00') {
        html += day + ':'
      }
      if (hour != '00') {
        html += hour + ':'
      }
      if (minute != '00') {
        html += minute + ':'
      }
      
      html += second + ':'
      html = html.slice(0, -1)
      $('.detail-header-timer').html(html)

      if(times <= 0) {
        $('.detail-header-timer').html('已过期')
        clearInterval(this.setInter)
      } else {
        times--
      }
    },1000)
  }

  Detail.prototype.getDetail = function () {
    var that = this
    $('.loader').show()
    $.ajax({
      type: 'get',
      url: '/detail/' + that.id,
      timeout: 10000,
      success: function(res) {
        if (res.code == '1000') {
          var dictList = that.dictList,
              response = res.data
          response.cargoTypeCode = that.valueFormat(dictList.cargoTypeCode, response.cargoTypeCode)
          response.packageTypeCode = that.valueFormat(dictList.packageTypeCode, response.packageTypeCode)
          response.vechicleTypeCode = that.valueFormat(dictList.carType, response.vechicleTypeCode)
          response.vechicleAttributeCode = that.valueFormat(dictList.carAttr, response.vechicleAttributeCode)

          that.lastModifyTime = +new Date(res.data.lastModifyTime.replace(/\-/g, '/'))
          that.countdown()
          var html = template('tpl-detail', {
            data: response
          })
          $('.detail-container').html(html)
        } else {
          alert('获取数据错误')
        }
        $('.loader').hide()
      },
      dataType: 'json',
      error: function(e) {
        $('.loader').hide()
      }
    })
  }

  Detail.prototype.valueFormat = function (dict, response) {
    var res = ''
    for (var i = 0, len = dict.length; i < len; i++) {
      if (response == dict[i].key) {
        res = dict[i].val
        break
      }
    }
    return res
  }

  // 屏幕自适应
  Detail.prototype.rem = function () {
    var win = window,
        doc = document,
        docEle = doc.documentElement,
        body = doc.body,
        resizeEvent = "orientationchange" in win ? "orientationchange" : "resize",
        recalc = function () {
          var clientWidth = body.offsetWidth  || docEle.clientWidth,
              clientHeight = body.offsetHeight  || docEle.clientHeight,
              isIPhone = win.navigator.appVersion.match(/iphone/gi),
              isAndroid = win.navigator.appVersion.match(/android/gi),
              devicePixelRatio = win.devicePixelRatio
          clientWidth = clientWidth >= 660 ? 660 : clientWidth
          // if ( isIPhone ) {
          //   devicePixelRatio = devicePixelRatio >= 3 ? 3 : devicePixelRatio
          // } else {
          //   devicePixelRatio = Math.floor(devicePixelRatio)
          // }
          devicePixelRatio = 2
          docEle.style.fontSize = Math.floor(devicePixelRatio * clientWidth / 10) + 'px'
        }

    win.addEventListener(resizeEvent, recalc, false)
    doc.addEventListener("DOMContentLoaded", recalc, false)
  }

  Detail.prototype.getDict = function () {
    var that = this
    $('.loader').show()
    $.ajax({
      type: 'get',
      url: '/get/dict',
      success: function(res) {
        if (res.code == '1000') {
          that.dictList = res.data
          that.getDetail()
        } else {
          alert('获取数据错误')
        }
        $('.loader').hide()
      },
      dataType: 'json',
      error: function(e) {
        $('.loader').hide()
      }
    })
  }

  var detail = new Detail()
  detail.init()
})()